package com.lemon.tankgame3;

/**
 * @author 软柠柠吖
 * @date 2022/1/25
 */
public class Shot implements Runnable {
    int x;// 子弹x坐标
    int y;// 子弹y坐标
    int direct = 0;// 子弹方向
    int speed = 2;// 子弹速度
    int type;
    boolean isLive = true;// 子弹是否存活
    Wall wall = null;

    // 构造器
    public Shot(int x, int y, int direct, int type, Wall wall) {
        this.x = x;
        this.y = y;
        this.direct = direct;
        this.type = type;
        this.wall = wall;
    }

    @Override
    public void run() {// 射击
        while (true) {
            // 线程休眠 50ms
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            // 根据方法来改变 x,y坐标
            switch (direct) {
                case 0:// 方向朝上
                    y -= speed;
                    break;
                case 1:// 方向朝右
                    x += speed;
                    break;
                case 2:// 方向朝下
                    y += speed;
                    break;
                case 3:// 方向朝左
                    x -= speed;
                    break;
                default:
                    break;
            }
            //System.out.println("子弹 x=" + x + " y=" + y);

            if (!(isLive && x >= 0 && x <= wall.x && y >= 0 && y <= wall.y)) {
                isLive = false;
                break;
            }
        }
    }
}
